/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import processor.SqlParser;

/**
 *
 * @author DYNG09
 */
public class LoadingDialog extends JDialog {

    private JPanel topPanel;
    private String query;
    private Socket s;
    private JTable tableResult;

    public LoadingDialog(Component main, String query, Socket s) {
        topPanel = new JPanel(new GridLayout());
        topPanel.add(new JLabel("   Executing. Please wait..."), new GridBagConstraints());
        this.getContentPane().add(topPanel);
        this.setSize(200, 100);
        this.setLocationRelativeTo(main);
        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.setModal(true);

        this.query = query;
        this.s = s;
        this.tableResult = null;
    }

    public void run() {
        SwingWorker<?, ?> worker = new SwingWorker<Void, Integer>() {

            @Override
            protected Void doInBackground() throws InterruptedException {
                //execute query
                executeStatement();
                return null;
            }

            protected void process(ArrayList<Integer> chunks) {
                //int selection = chunks.get(chunks.size() - 1);
                //btn.setText("long task up to " + selection + "%");
            }

            @Override
            protected void done() {
                close();
            }
        };
        worker.execute();
        this.setVisible(true);
    }

    private void executeStatement() {
        //parse sql
        SqlParser parser = new SqlParser(this.query);
        Document doc = parser.parse();
        //send-receive doc to-from server
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            oos = new ObjectOutputStream(s.getOutputStream());
            ois = new ObjectInputStream(s.getInputStream());
            //send
            oos.writeObject(doc);
            //recv
            doc = (Document) ois.readObject();
            if ( doc != null )
            {
            try
            {
                TransformerFactory transfac = TransformerFactory.newInstance();
                Transformer trans = transfac.newTransformer();
                trans.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes");
                trans.setOutputProperty( OutputKeys.INDENT, "yes");
                
                File file = new File("D:\\recv.xml");
                StreamResult res = new StreamResult(file);
                DOMSource source = new DOMSource(doc);
                trans.transform(source, res);
            }
            catch ( Exception ex) {  }
            }
            
            
            this.setTableResult(parser.parseInverse(doc));
            
        } catch (IOException ex) {
            Logger.getLogger(LoadingDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LoadingDialog.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void close() {
        this.dispose();
    }

    /**
     * @return the tableResult
     */
    public JTable getTableResult() {
        return tableResult;
    }

    /**
     * @param tableResult the tableResult to set
     */
    public void setTableResult(JTable tableResult) {
        this.tableResult = tableResult;
    }
}
